import Vue from 'vue'
import Router from 'vue-router'
import Movie from './movie'
import Mine from './mine'
import Admin from './admin'
import Cinema from './cinema'
import axios from 'axios'
Vue.use(Router)
const router = new Router({
  mode: 'history',
  // base: process.env.NODE_ENV === 'production' ? process.env.BASE_URL : 'miaomiao',
  // base: 'miaomiao',
  routes: [
    {
      path: '/',
      redirect: '/movie'
    },
    Movie,
    Mine,
    Cinema,
    Admin
  ]
})
router.beforeEach(async (to, from, next) => {
  if (to.path === '/mine/login') return next()
  let res = await axios.get('/api2/users/getUser')
  if (to.path === '/mine/center') {
    if (res.status === 0) {
      return next()
    } else {
      return next('/mine/login')
    }
  }
  if (to.path === '/admin') {
    if (res.data.role === 'admin') {
      return next()
    } else {
      return next('/mine/login')
    }
  }
  next()
})
export default router
